/**
 * 
 */
package edu.gatech.cc.cnc.params;

import java.math.BigInteger;
import java.util.LinkedHashSet;
import java.util.Set;

import client.Cut;
import client.Needed;

import edu.gatech.cc.jcrasher.Constants.Visibility;
import edu.gatech.cc.jcrasher.planner.TypeNeededNode;
import edu.gatech.cc.jcrasher.plans.expr.Expression;
import edu.gatech.cc.jcrasher.plans.expr.literals.NullLiteral;
import edu.gatech.cc.jcrasher.types.TypeGraph;
import edu.gatech.cc.jcrasher.types.TypeGraphImpl;
import junit.framework.TestCase;
import static edu.gatech.cc.jcrasher.Assertions.check;
import static edu.gatech.cc.jcrasher.Assertions.notNull;


/**
 * @author Christoph Csallner
 */
public class JCrasherPlannerTest extends TestCase {

  final TypeGraph typeGraph = TypeGraphImpl.instance();
  final JCrasherPlanner planner = JCrasherPlanner.instance();
  
  @Override
  protected void setUp() throws Exception {
    super.setUp();
    
    Set<Class<?>> classes = new LinkedHashSet<Class<?>>();
    classes.add(Needed.class);
    typeGraph.crawl(classes, Visibility.GLOBAL);
  }
  
  
  public void testDefault() {
    assertTrue(planner.hasDefault(Object.class));
    
    assertFalse(planner.hasDefault(Needed.class));
    assertFalse(planner.hasDefault(Cut.class));
  }
  
  
  public void testGetDefault() {
    Expression<? extends Object> plan;
    plan = planner.getDefault(Object.class);
    notNull(plan);
    check(plan.text().length() > 0);
  }
  
  public void testGetRandom() {
    Expression<? extends Needed> plan;

    for (int i=0; i<1000; i++) {
      plan = planner.getRandom(Needed.class, Cut.class);
      notNull(plan);
      check(plan.text().length() > 0);
      check(!(plan instanceof NullLiteral));
      //System.out.println(plan.text());
    }
  }
  
  
  public void testGetPlanSpace() {
    TypeNeededNode<Needed> typeNode = planner.getPlanSpace(Needed.class);
    BigInteger size = typeNode.getPlanSpaceSize();
    check(size.compareTo(BigInteger.valueOf(12)) == 0);
    
    Expression<? extends Needed> plan;
    plan = typeNode.getPlan(BigInteger.valueOf(0), Cut.class);
    assertEquals("new Needed(new java.lang.Object())", plan.text());
    
    for (int i=0; i<12; i++)
      System.out.println(typeNode.getPlan(BigInteger.valueOf(i), Cut.class).text());    
  }
}
